package tcp;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class AESEncryptionExample {

    public static void main(String[] args) throws Exception {
        //要加密的内容
        String plainText = "{\n" +
                "\t\"dataId\": \"90134c28c31b49ea85e17bb90ff32eef\",\n" +
                "\t\"enterpriseId\": \"123456789\",\n" +
                "\t\"gatewayId\": \"123456789\",\n" +
                "\t\"collectTime\": \"20180615123456\",\n" +
                "\t\"isConnectDataSource\": true,\n" +
                "\t\"reportType\": \"report\",\n" +
                "\t\"datas\": [\n" +
                "\t\t{\n" +
                "\t\t\t\"quotaId\": \"123\",\n" +
                "\t\t\t\"value\": 123.0,\n" +
                "\t\t\t\"isValid\": true\n" +
                "\t\t},\n" +
                "\t\t{\n" +
                "\t\t\t\"quotaId\": \"321\",\n" +
                "\t\t\t\"value\": 123.0,\n" +
                "\t\t\t\"isValid\": true\n" +
                "\t\t}\n" +
                "\t]\n" +
                "}";
//        密钥
        String key = "d1283736930b4c55";
//        偏移量
        String iv = "0000000000000000";
        byte[] encryptedBytes = encrypt(plainText, key);
        String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes);
        System.out.println(encryptedText);
//        String decryptedText = decrypt(encryptedBytes, key, iv);
//        System.out.println("Decrypted Text: " + decryptedText);
    }


    public static byte[] encrypt(String plainText, String key) throws Exception {
        String iv = "0000000000000000";
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES");
//        IvParameterSpec ivParameterSpec = new IvParameterSpec(hexStringToByteArray(iv));
        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
        return cipher.doFinal(plainText.getBytes("UTF-8"));
    }
    public static String decrypt(byte[] encryptedBytes, String key, String iv) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES");
//        IvParameterSpec ivParameterSpec = new IvParameterSpec(hexStringToByteArray(iv));
        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
        byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
        return new String(decryptedBytes, "UTF-8");
    }
    public static byte[] hexStringToByteArray(String hexString) {
        int len = hexString.length();
        byte[] byteArray = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            byteArray[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4)
                    + Character.digit(hexString.charAt(i + 1), 16));
        }
        return byteArray;
    }
}